﻿@using TbShop.Web.Helpers;
@using TbShop;
@using PagedList.Mvc;
@{
    ViewBag.Title = "管理员管理";
    ViewBag.ModuleId = "Administrator";
    var name = Request.QueryString["name"];
    var isAdd = AuthorizationHelper.IsAuthorised(Constants.AuthorityIdentity.AddAdministrator);
    var isResetPwd = AuthorizationHelper.IsAuthorised(Constants.AuthorityIdentity.ResetAdminPwd);
    var isEdit = AuthorizationHelper.IsAuthorised(Constants.AuthorityIdentity.EditAdministrator);
    var isDel = AuthorizationHelper.IsAuthorised(Constants.AuthorityIdentity.DeleteAdministrator);
}
@Html.AntiForgeryToken()
<input type="hidden" name="hname" value="@name" />
<div class="panel panel-default">
    <div class="panel-heading">
        <strong>管理员管理</strong>
        <div class="pull-right">
            @if (isAdd)
            {
                <a href="@(Url.Action("Edit"))"><span class='glyphicon glyphicon-plus' aria-hidden='true'></span> 添加新管理员</a>
            }
        </div>
    </div>
    <div class="panel-body">
        <nav class="navbar navbar-default">
            <div class="container-fluid">
                <div class="collapse navbar-collapse">
                    <form class="navbar-form navbar-left" method="get" id="searchForm">
                        <div class="form-group">
                            <label for="Name">管理员：</label>
                            <input type="text" placeholder="名称/账号" name="name" class="form-control span3" value="@name" />
                        </div>&nbsp;
                        <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i> 查询</button>
                    </form>
                </div>
            </div>
        </nav>
        <table id="user_table"></table>
    </div>
</div>

@using (Html.BeginForm("Delete", "Administrator", FormMethod.Post, new { id = "DeleteForm" }))
{
    @Html.AntiForgeryToken()
    <input type="hidden" name="id" value="0" />
}
@using (Html.BeginForm("ResetPassword", "Administrator", FormMethod.Post, new { id = "ResetPasswordForm" }))
{
    @Html.AntiForgeryToken()
    <input type="hidden" name="id" value="0" />
}
@using (Html.BeginForm("ChangeStatus", "Administrator", FormMethod.Post, new { id = "ChangeStatusForm" }))
{
    @Html.AntiForgeryToken()
    <input type="hidden" name="id" value="0" />
    <input type="hidden" name="type" value="0" />
}
@section scripts{
    @Styles.Render("~/bundles/editable-table-css")
    @Scripts.Render("~/bundles/editable-table")
    <script type="text/javascript">
        function reset(id) {
            $("#ResetPasswordForm input[name='id']").val(id);
            dialog({
                title: "确认重置",
                content: "是否确定要重置该用户的密码？默认密码为" + "@(HeinQi.Framework.Common.SystemParameterHelper.GetSystemParameter<string>(TbShop.Constants.SystemParameterKeys.AdministratorDefaultPassword))",
                width: "200",
                okValue: "确认",
                ok: function () {
                    $("#ResetPasswordForm").submit();
                },
                cancelValue: "取消",
                cancel: function () {
                }
            }).showModal();
        }

        function deleted(id) {
            $("#DeleteForm input[name='id']").val(id);
            dialog({
                title: "确认删除",
                content: "是否确认删除该管理员？",
                width: "200",
                okValue: "确认",
                ok: function () {
                    $("#DeleteForm").submit();
                },
                cancelValue: "取消",
                cancel: function () {
                }
            }).showModal();
        }

        function changeStatus(id, type) {
            var title = '禁用';
            if (type == 1) {
                title = '启用';
            }
            $("#ChangeStatusForm input[name='id']").val(id);
            $("#ChangeStatusForm input[name='type']").val(type);
            dialog({
                title: "确认" + title,
                content: "是否确认" + title + "该管理员？",
                width: "200",
                okValue: "确认",
                ok: function () {
                    $("#ChangeStatusForm").submit();
                },
                cancelValue: "取消",
                cancel: function () {
                }
            }).showModal();
        }

        var roleList = JSON.parse('@(Html.Raw(ViewBag.RoleList))');

        $(function () {
            $('#user_table').bootstrapTable({
                columns: [{
                    title: '头像',
                    field: 'HeadPortrait',
                    formatter: function (value) {
                        return '<img src="' + value + '" width="50" height="50" onerror="imageError($(this));"/>';
                    }
                }, {
                    title: '账号名称',
                    field: 'UserName'
                }, {
                    title: '登录帐号',
                    field: 'Name'
                },
                {
                    title: '联系方式',
                    field: 'PhoneNumber'
                }, {
                    title: '所属角色',
                    field: 'RoleId',
                    formatter: function (value) {
                        var find = $.grep(roleList, function (n) {
                            return n.value == value;
                        });
                        if (find.length > 0) {
                            return find[0].text;
                        }
                        else {
                            return "-";
                        }
                    }
                },
                {
                    title: '添加时间',
                    field: 'CreatedDate',
                    formatter: function (value) {
                        return value.format("yyyy-MM-dd hh:mm");
                    }
                },
                {
                    title: '操作',
                    field: 'Id',
                    formatter: function (value, row, index) {
                        return ('@(isResetPwd ? "true" : "false")' == 'true' ? '<a href="javascript:void(0)" onclick="reset(\'' + value + '\')"><span class="glyphicon glyphicon-refresh" aria-hidden="true"></span>重置密码</a>&nbsp;&nbsp;' : '') +
                        ('@(isEdit ? "true" : "false")' == 'true' ? '<a href="@(Url.Action("Edit"))?id=' + value + '"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span>修改</a>&nbsp;&nbsp;' : '') +
                        ('@(isDel ? "true" : "false")' == 'true' ? '<a href="javascript:void(0)"  onclick="deleted(' + value + ')"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span>删除</a>' : '');
                    }
                }
                ],
                idField: 'Id',
                pagination: true,
                sidePagination: 'server',
                url: '@Url.Action("GetUserList")',
                queryParamsType: '',
                queryParams: function (params) {
                    params["name"] = $('input[name="hname"]').val();
                    return params;
                },
                locale: 'zh-CN'
            });
        })
    </script>
}